#coding=utf-8
#!/usr/bin/python
from sgmllib import SGMLParser

class JumpObject:
    def __init__(self,jUrl,jDesc):
        self.jumpUrl = jUrl
        self.jumpDesc=jDesc
    def __del__(self):
        pass


class parserXml(SGMLParser):

       def __init__(self):
            SGMLParser.__init__(self)
            self.hrefs = []
            self.is_a=""
            self.smallUrl = []

            self.jumpObj =[]


      #获取超链接href值
       def start_a(self, attrs):
            href = [v for k, v in attrs if k == "href"]
            self.hrefs.extend(href)
            self.is_a=href
       def end_a(self):
           self.is_a=""

       def handle_data(self, text):
            if self.is_a:
                self.smallUrl.append(text)
                self.smallUrl.append(self.is_a)
                self.smallUrl.append('\n')

                jobj = JumpObject(self.is_a,text)
                self.jumpObj.append(jobj)

       def getJumpObjs(self):
           "get jump objs"
           return self.jumpObj

 #写入到文件中
def SpWriteHrefFile(filepath,parser):
    hrefs=open(filepath,"w")
    #分行显示,链接
    for href in parser.hrefs:
       hrefs.write("%s%s" % (href,"\n"))
    hrefs.close()

def SpWriteHrefAndDescFile(filepath,parser):
    hrefs=open(filepath,"w")
    for href in parser.smallUrl:
       hrefs.write("%s%s" % (href,"\n"))
    hrefs.close()